#region Using

using System;
using System.Configuration;
using System.Web.Security;
using System.Linq;
using Gizmox.WebGUI.Forms.Authentication;
using System.Collections.Generic;
using PLACEHR.Web.Utilities;

#endregion

namespace PLACEHR.Web
{
    public partial class LoginForm : LogonForm
    {
        /// <summary>
        /// Creates a new <see cref="LogonForm"/> instance.
        /// </summary>
        public LoginForm()
        {
            InitializeComponent();
        }

        private void InitializeData(string languageCode)
        {
            if (string.IsNullOrWhiteSpace(languageCode))
            {
                languageCode = Global.LanguageCode;
            }
            this.Text = LanLayer.GetTrans(81, languageCode);
            lblUsername.Text = LanLayer.GetTrans(78, languageCode);
            lblpwd.Text = LanLayer.GetTrans(79, languageCode);
            cbRemember.Text = LanLayer.GetTrans(80, languageCode);
            btnLogin.Text = LanLayer.GetTrans(82, languageCode);
            btnCancel.Text = LanLayer.GetTrans(19, languageCode);
            //rbChinese.Text = LanLayer.GetTrans(30, languageCode);
            //rbEnglish.Text = LanLayer.GetTrans(31, languageCode);
        }

        #region Event Handlers

        private void Form_Load(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.Session["DataContext"] == null)
            {
                System.Web.HttpContext.Current.Session["DataContext"] = new HRDBEntities();
            }

            InitializeData(null);
            //PLACEHRCore context = new PLACEHRCore();

            //Context["DBContext"] = context;
            if (Context.Cookies["Language"] != null)
            {
                if (Context.Cookies["Language"] == Constants.EnCode)
                {
                    rbEnglish.Checked = true;
                }
                else
                {
                    rbChinese.Checked = true;
                }
            }
            else
            {
                string lng = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"].ToString();
                List<string> lstLng = lng.Split(',').ToList();
                if (lstLng.Count > 0)
                {
                    if (lstLng[0].ToLower().Contains(Constants.ZhCode))
                    {
                        Utility.SetRequestCookie("Language", Constants.ZhCode);
                        Context.Cookies["Language"] = Constants.ZhCode;
                        rbChinese.Checked = true;
                    }
                    else
                    {
                        Utility.SetRequestCookie("Language", Constants.EnCode);
                        Context.Cookies["Language"] = Constants.EnCode;
                        rbEnglish.Checked = true;
                    }
                }
            }

            if (!string.IsNullOrEmpty(Context.Cookies["Username"]) && !string.IsNullOrEmpty(Context.Cookies["Password"]))
            {
                tbxUsername.Text = Context.Cookies["Username"];
                tbxPassword.Text = Context.Cookies["Password"];
                cbRemember.Checked = true;
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Context.Session.IsLoggedOn = false;
            lblMessage.Text = string.Empty;
            tbxUsername.Text = string.Empty;
            tbxPassword.Text = string.Empty;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.Session["DataContext"] == null)
            {
                System.Web.HttpContext.Current.Session["DataContext"] = new HRDBEntities();
            }
            
            if (cbRemember.Checked)
            {
                Context.Cookies["Username"] = tbxUsername.Text;
                Context.Cookies["Password"] = tbxPassword.Text;
            }
            else
            {
                Context.Cookies["Username"] = string.Empty;
                Context.Cookies["Password"] = string.Empty;
            }

            if (rbChinese.Checked)
            {
                // set response cookie
                Context.Cookies["Language"] = Constants.ZhCode;

                //set request cookie
                Utility.SetRequestCookie("Language", Constants.ZhCode);
            }
            else
            {
                // set response cookie
                Context.Cookies["Language"] = Constants.EnCode;

                //set request cookie
                Utility.SetRequestCookie("Language", Constants.EnCode);
            }


            string username = tbxUsername.Text;
            string password = tbxPassword.Text;

            var userList = (from c in Global.Context.Employees
                            where (c.ChineseName.ToLower() == tbxUsername.Text.Trim().ToLower() || c.EnglishName.ToLower() == tbxUsername.Text.Trim().ToLower()) && c.Password == tbxPassword.Text && c.IsDeleted == false
                            select c);
            if (userList != null && userList.Count() > 0)
            {
                Employee currUser = userList.First();
                if (Global.UserCollection.ContainsValue(currUser))
                {
                    Global.UserCollection.Remove(Global.UserCollection.First(a => a.Value == currUser).Key);
                }
                if (Global.UserCollection.ContainsKey(Session.SessionID))
                    Global.UserCollection[Session.SessionID] = currUser;
                else
                    Global.UserCollection.Add(Session.SessionID, currUser);
                System.Web.HttpContext.Current.Session["CurrUser"] = currUser;

                Context.Session.IsLoggedOn = true;
                //Context.CurrentUICulture = ((LanguageOption)mobjComboLanguage.SelectedItem).Culture;
                lblMessage.Text = "";
            }
            else
            {
                lblMessage.Text = "Incorrect user name or password.";
                Context.Session.IsLoggedOn = false;
            }
            //Context.Session.IsLoggedOn = true;
        }

        #endregion

        private void button2_KeyDown(object objSender, Gizmox.WebGUI.Forms.KeyEventArgs objArgs)
        {

        }

        private void mobjTextPassword_KeyUp(object objSender, Gizmox.WebGUI.Forms.KeyEventArgs objArgs)
        {

        }

        private void rbChinese_CheckedChanged(object sender, EventArgs e)
        {
            string currentLanCode = rbChinese.Checked ? Constants.ZhCode : Constants.EnCode;
            InitializeData(currentLanCode);
        }

    }
}